package servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/test")
public class MyFilterA implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init");
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter-b");
        // 是否登录
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpSession session = request.getSession();
//        User user = (User) session.getAttribute("user");
//        if (user != null) {
//            // 放行
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else {
//            // 转发或者重定向登录页面
//        }

        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("doFilter-b");

    }

    @Override
    public void destroy() {
        System.out.println("destroy");
        Filter.super.destroy();
    }
}
